package com.grq.mis.validate;

import com.grq.mis.entity.User;
import com.grq.mis.exception.BusinessException;
import com.grq.mis.exception.ResultEnum;
import org.springframework.util.StringUtils;

public class UserValidate {

    public static void add(User user) {

        // username
        if (StringUtils.isEmpty(user.getUsername())) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "账号不能为空");
        }
        if (user.getUsername().length() > 36) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "账号多为36字符");
        }

        // name
        if (StringUtils.isEmpty(user.getName())) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "姓名不能为空");
        }
        if (user.getName().length() > 36) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "姓名最多为36字");
        }

        // password
        if (StringUtils.isEmpty(user.getPassword())) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "用户密码不能为空");
        }
        if (user.getPassword().length() < 6 || (user.getPassword().length() > 32)) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "用户密码长度为6-32");
        }

        // 岗位
        if (!StringUtils.isEmpty(user.getCompany())){
            if (user.getCompany().length() > 50) {
                throw new BusinessException(ResultEnum.PARAM_ERROR, "岗位最多为50字");
            }
        }

        // 手机号
        if (StringUtils.isEmpty(user.getPhone())) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "手机号");
        }
        if (user.getPhone().length() > 11) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "手机号最多为11字");
        }

        if (user.getDepartmentId() == null){
            throw new BusinessException(ResultEnum.PARAM_ERROR, "请选择商户");
        }


    }

    public static void update(User user) {

        if (user.getId() == null){
            throw new BusinessException(ResultEnum.PARAM_ERROR, "用户id不能为空");
        }

        // name
        if (StringUtils.isEmpty(user.getName())) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "姓名不能为空");
        }
        if (user.getName().length() > 36) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "姓名最多为36字");
        }

        // 岗位
        if (!StringUtils.isEmpty(user.getCompany())){
            if (user.getCompany().length() > 50) {
                throw new BusinessException(ResultEnum.PARAM_ERROR, "岗位最多为50字");
            }
        }

        // 手机号
        if (StringUtils.isEmpty(user.getPhone())) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "手机号");
        }
        if (user.getPhone().length() > 11) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "手机号最多为11字");
        }

        if (user.getDepartmentId() == null){
            throw new BusinessException(ResultEnum.PARAM_ERROR, "请选择商户");
        }

        if (user.getIsDisable() == null){
            throw new BusinessException(ResultEnum.PARAM_ERROR, "账号状态不能为空");
        }

    }

    public static void register(User user) {
        // username
        if (StringUtils.isEmpty(user.getUsername())) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "账号不能为空");
        }
        if (user.getUsername().length() > 36) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "账号多为36字符");
        }

        // name
        if (StringUtils.isEmpty(user.getName())) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "姓名不能为空");
        }
        if (user.getName().length() > 36) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "姓名最多为36字");
        }

        // password
        if (StringUtils.isEmpty(user.getPassword())) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "用户密码不能为空");
        }
        if (user.getPassword().length() < 6 || (user.getPassword().length() > 32)) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "用户密码长度为6-32");
        }

        // 岗位
        if (!StringUtils.isEmpty(user.getCompany())){
            if (user.getCompany().length() > 50) {
                throw new BusinessException(ResultEnum.PARAM_ERROR, "岗位最多为50字");
            }
        }

        // 手机号
        if (StringUtils.isEmpty(user.getPhone())) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "手机号");
        }
        if (user.getPhone().length() > 11) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "手机号最多为11字");
        }

        if (user.getDepartmentId() == null){
            throw new BusinessException(ResultEnum.PARAM_ERROR, "请选择商户");
        }
    }

    public static void updateMyInfo(User user) {
        // name
        if (StringUtils.isEmpty(user.getName())) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "姓名不能为空");
        }
        if (user.getName().length() > 36) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "姓名最多为36字");
        }

        // 岗位
        if (!StringUtils.isEmpty(user.getCompany())){
            if (user.getCompany().length() > 50) {
                throw new BusinessException(ResultEnum.PARAM_ERROR, "岗位最多为50字");
            }
        }

        // 手机号
        if (StringUtils.isEmpty(user.getPhone())) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "手机号");
        }
        if (user.getPhone().length() > 11) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "手机号最多为11字");
        }
    }

    /*public static void update(User user) {
        if (user.getId() == null) {
            throw new GrqException(ResultEnum.PARAM_ERROR, "账号ID不能为空");
        }

        // name
        if (StringUtils.isEmpty(user.getName())) {
            throw new GrqException(ResultEnum.PARAM_ERROR, "姓名不能为空");
        }
        if (user.getName().length() > 36) {
            throw new GrqException(ResultEnum.PARAM_ERROR, "姓名最多为36字");
        }

        if (user.getIsDisable() == null) {
            throw new GrqException(ResultEnum.PARAM_ERROR, "账号状态不能为空");
        }

        // phone
        if (StringUtils.isEmpty(user.getPhone()) == false) {
            if (user.getPhone().length() > 11) {
                throw new GrqException(ResultEnum.PARAM_ERROR, "手机号最多为11字");
            }
        }
    }*/
}
